package de.ur.iw.wiki.test;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.ur.iw.wiki.Link;
import de.ur.iw.wiki.LinkInfo;
import de.ur.iw.wiki.Paragraph;
import de.ur.iw.wiki.WikiDownloader;
import de.ur.iw.wiki.WikiPage;

public class DownloadTester {

	public static void main(String[] args) {
		try {
			WikiPage page = pageTest();
			print(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static WikiPage pageTest() throws ParserConfigurationException,
			SAXException, IOException {
		return WikiDownloader.download("Schwabach");//"Unterreichenbach_(Schwabach)");
	}

	public static WikiPage categoryTest() throws ParserConfigurationException,
			SAXException, IOException {
		return WikiDownloader
				.downloadCategory("Kategorie:Informationswissenschaft");
	}

	public static void print(WikiPage page) {
		System.out.println("there are " + page.size() + " links in '"
				+ page.getName() + "'\n" + "in " + page.getParagraphCount()
				+ " Paragraphs:");

		for (int i = 0; i < page.getParagraphCount(); i++) {
			Paragraph p = page.getParagraphAt(i);
			printParagraph(p, 1);
		}

		System.out.println("containing " + page.getFileCount() + " files:");
		for (int i = 0; i < page.getFileCount(); i++) {
			Link link = page.getFileAt(i);
			int position = link.getInfo().getInt(
					LinkInfo.KEY_POSITION_IN_PARAGRAPH);
			int pPos = link.getInfo()
					.getInt(LinkInfo.KEY_POSITION_OF_PARAGRAPH);
			System.out.println("\t" + link.getName() + " (" + position
					+ "% from start in a paragraph " + pPos
					+ "% from pagestart)");
		}

		System.out.println("and " + page.getCategoryCount() + " categories:");
		for (int i = 0; i < page.getCategoryCount(); i++) {
			Link link = page.getCategoryAt(i);
			int position = link.getInfo().getInt(
					LinkInfo.KEY_POSITION_IN_PARAGRAPH);
			int pPos = link.getInfo()
					.getInt(LinkInfo.KEY_POSITION_OF_PARAGRAPH);
			System.out.println("\t" + link.getName() + " (" + position
					+ "% from start in a paragraph " + pPos
					+ "% from pagestart)");
		}
	}

	private static void printParagraph(Paragraph p, int deepness) {
		String tabs = "";
		for (int i = 0; i < deepness; i++) {
			tabs += "\t";
		}
		System.out.println(tabs + p.getName() + " (" + p.getAllLinks().size()
				+ " links)");
		if (p.getParagraphCount() > 0) {
			for (int i = 0; i < p.getParagraphCount(); i++) {
				printParagraph((Paragraph) p.getParagraphAt(i), deepness + 1);
			}
		} else {
			for (int i = 0; i < p.getAllLinks().size(); i++) {
				Link link = p.getAllLinks().get(i);
				int position = link.getInfo().getInt(
						LinkInfo.KEY_POSITION_IN_PARAGRAPH);
				int pPos = link.getInfo().getInt(
						LinkInfo.KEY_POSITION_OF_PARAGRAPH);
				System.out.println(tabs + "\t" + link.getName() + " ("
						+ position + "% from start in a paragraph " + pPos
						+ "% from pagestart)");
			}
		}
	}

}
